Data Message Callback
7. dataMessageCallback
-
Purpose:
Transfers data from the server side to the client application. -
Functionality:
- Parses the JSON payload from the incoming message.
- Uses the
model.url
value to determine how to process the payload. - Depending on the endpoint (e.g.,
Onboarding
,IdentityValidation.dl
,VoiceIdentity
), it updates the user state, dispatches Redux actions, logs errors, or emits events.
-
Usage:
Acts as a central handler for various data messages, ensuring that each type of message is processed appropriately and that the application responds correctly to server-side events.
Example:
dataMessageCallback: async (model) => {
// Parse the incoming payload from the server
const payloadObject = JSON.parse(model.payload);
console.log('Data message received:', model);
// Handle different message types based on the URL endpoint
switch (model.url) {
case 'Onboarding':
if (payloadObject.success) {
// Create a basic user object from the payload data
const userData = {
name: payloadObject.name,
email: payloadObject.email,
phone: payloadObject.phone
};
// Dispatch action to create/update the user
}
break;
case 'IdentityValidation.dl':
// Build an OCR response from the payload
const ocrResponse = {
ocr: payloadObject.ocr,
faceBase64: payloadObject.faceBase64,
success: payloadObject.success,
isWasLastAttempt: payloadObject.isWasLastAttempt
};
// Emit an event with the OCR data and quality check result
eventEmitter.emit('imageQualityChecked', {
requestId: model.requestId,
ocrData: ocrResponse.ocr,
faceBase64: ocrResponse.faceBase64,
isGoodQuality: ocrResponse.success,
code: payloadObject.errorCode
});
break;
case 'IdentityValidation.selfie':
if (!payloadObject.success) {
// Emit an event if face matching fails, with specific handling for error code '402'
if (payloadObject.errorCode === '402') {
eventEmitter.emit('FaceMatchingFailed', {
requestId: model.requestId,
ocrData: null,
isGoodQuality: payloadObject.success,
code: payloadObject.errorCode
});
}
// For other errors (except a specific code), also emit a failure event
if (payloadObject.errorCode !== '418') {
eventEmitter.emit('FaceMatchingFailed', {
requestId: model.requestId,
ocrData: payloadObject.ocr,
isGoodQuality: payloadObject.success,
code: payloadObject.errorCode
});
}
}
break;
case 'VoiceIdentity':
case 'VoiceIdentity.record2':
// Emit an event to indicate the voice biometric check result
eventEmitter.emit('VoiceBiometricChecked', {
requestId: model.requestId,
isGoodQuality: payloadObject.success,
code: payloadObject.errorCode
});
break;
case 'IdentityValidation.started':
// Store the instance ID and log it for later use in the workflow
const instanceId = payloadObject.instanceId;
AsyncStorage.setItem('instanceId', instanceId);
console.log('Instance ID:', instanceId);
break;
case 'IdentityValidation.email':
console.log('ValidateEmail:', payloadObject);
break;
default:
// No action for unhandled URLs
break;
}
},
This generic documentation and example provide a clear overview of the dataMessageCallback
function. It explains how the function parses the payload, processes different endpoints, and updates the application's state accordingly. Developers can customize the handling for each case to suit their specific application requirements.